package com.example.springboot.web;


import com.example.springboot.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ThymeleafController {


    @RequestMapping(value = "/thymeleaf/index")
    public String index(Model model) {


        model.addAttribute("data","spingboot成功集成Thymeleaf模板");

        return "index";
    }


    @RequestMapping(value = "/thymeleaf/user")
    public String user(Model model) {

        User user = new User();
        user.setId(1);
        user.setName("张三");
        user.setPhone("1523535235263");
        user.setAddress("北京大前门");

        model.addAttribute("user",user);
        return "user";
    }


    @RequestMapping(value = "/thymeleaf/url")
    public String url(Model model) {
        User user = new User();
        user.setId(2);
        user.setName("赵六");
        user.setPhone("13800000000");
        user.setAddress("上海市");
        model.addAttribute("user",user);
        return "url";
    }
    @RequestMapping(value = "/thymeleaf/info")
    public String info(Integer id) {
        System.out.println("用户编号：" + id);
        return "info";
    }


    @RequestMapping("/each/list")
    public String eachList(Model model) {
        List<User> userList = new ArrayList<>();

        for (int i = 0; i<10; i++) {
            User user = new User();
            user.setId(100+i);
            user.setName("张"+i);
            user.setPhone("1361234567" + i);
            user.setAddress("北京市大兴区" + i);
            userList.add(user);
        }

        model.addAttribute("userList",userList);

        return "each";
    }


    @RequestMapping(value = "/condition")
    public String condition(HttpServletRequest request, Model model) {
        User user1 = null;
        model.addAttribute("user1", user1);
        User user2 = new User();
        user2.setId(1001);
        user2.setName("小岳岳");
        user2.setPhone("13900000000");
        user2.setAddress("北京市");
        model.addAttribute("user2", user2);
        model.addAttribute("sex", 1);
        User user3 = new User();
        user3.setId(1002);
        user3.setName("孙悦");
        user3.setPhone("13200000000");
        user3.setAddress("北京市");
        model.addAttribute("user3", user3);
        request.getSession().setAttribute("user3", user3);
        return "condition";
    }




}
